import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/common/color_common.dart';
import 'package:gzmall_app/common/net_util.dart';
import 'package:gzmall_app/model/logistics.dart';

class LogisticsPage extends StatefulWidget {
  int id;
  LogisticsPage(this.id);
  @override
  State<StatefulWidget> createState() => LogisticsPageState(this.id);
}

class LogisticsPageState extends State<LogisticsPage> {
  int id;
  List<Logistics> _logistics = [];

  LogisticsPageState(this.id);

  @override
  void initState() {
    super.initState();
    _getExpressTracks(this.id.toString());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _createAppBar(),
      body: ListView.builder(
        itemCount: _logistics.length,
        itemBuilder: (BuildContext context, int index) {
          return _createLogisticsItem(
              _logistics[index],
              index == 0 ? ColorCommon.primaryColor : ColorCommon.black1,
              index == 0 ? ColorCommon.primaryColor : ColorCommon.black1);
        },
      ),
    );
  }

  _createAppBar() {
    return AppBar(
      elevation: 0.0,
      title: Text("物流信息"),
      leading: IconButton(
        icon: Icon(Icons.arrow_back_ios),
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
    );
  }

  _createLogisticsHeader() {
    return Container(
      color: Colors.white,
      margin: const EdgeInsets.only(top: 1.0),
      padding: const EdgeInsets.only(
          left: 15.0, right: 15.0, top: 15.0, bottom: 15.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            alignment: Alignment.bottomCenter,
            width: 80.0,
            height: 80.0,
            decoration: BoxDecoration(
                border: Border.all(width: 1.0, color: ColorCommon.grey3),
                image: DecorationImage(
                    fit: BoxFit.fill,
                    image: CachedNetworkImageProvider(
                        "http://via.placeholder.com/350x150"))),
            child: Container(
              width: 80.0,
              color: ColorCommon.translucent2,
              padding: const EdgeInsets.only(top: 4.0, bottom: 4.0),
              child: Text(
                "共1件商品",
                textAlign: TextAlign.center,
                style: TextStyle(fontSize: 14.0, color: Colors.white),
              ),
            ),
          ),
          Expanded(
            child: Container(
              height: 80.0,
              margin: const EdgeInsets.only(left: 10.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text(
                    "物流状态: 运输中",
                    style: TextStyle(fontSize: 16.0, color: ColorCommon.black1),
                  ),
                  Text(
                    "快递公司: 顺丰快递",
                    style: TextStyle(fontSize: 16.0, color: ColorCommon.black1),
                  ),
                  Text(
                    "快递单号: 43578437592332",
                    style: TextStyle(fontSize: 16.0, color: ColorCommon.black1),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  _createLogisticsItem(
      Logistics logistice, Color contentColor, Color timeColor) {
    return Container(
      height: 60.0,
      padding: const EdgeInsets.only(left: 10.0, right: 10.0),
      color: Colors.white,
      child: Row(children: <Widget>[
        // Image.asset(
        //   "images/fun_1.png",
        //   width: 20.0,
        //   height: 60.0,
        //   fit: BoxFit.fill,
        // ),
        Expanded(
          child: Container(
            margin: const EdgeInsets.only(top: 5.0, bottom: 5.0),
            decoration: BoxDecoration(color: ColorCommon.grey5),
            padding: const EdgeInsets.only(left: 15.0, right: 15.0),
            child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    logistice?.content ?? "",
                    style: TextStyle(fontSize: 14.0, color: contentColor),
                  ),
                  Text(
                    logistice?.createdAt ?? "",
                    style: TextStyle(fontSize: 12.0, color: timeColor),
                  ),
                ]),
          ),
        ),
      ]),
    );
  }

  _getExpressTracks(String id) {
    Map<String, String> params = {
      "order_id": id,
    };

    NetUtil.get(context, Api.expressTracks, (data, message) {
      if (!mounted) return;
      setState(() {
        _logistics = (data as List)
            ?.map((e) => e == null ? null : Logistics.fromJson(e))
            ?.toList();
      });
    }, params: params);
  }
}
